package com.life.view;

import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JList;

public class ListAdapter extends AbstractAdapter
{
    JList theList = null;
    
    public ListAdapter(JList list)
    {
	theList = list;
	theList.setModel(new DefaultListModel());
    }

    @Override
    public Object get()
    {
	return theList.getSelectedValue();
    }

    @Override
    public void set(Object data)
    {
	((DefaultListModel)theList.getModel()).clear();
	
	ArrayList listData = (ArrayList)data;
	int size = listData.size();
	
	for(int i = 0; i < size; i++)
	{
	       ((DefaultListModel)theList.getModel()).addElement(listData.get(i));
	}
	
	if(size > 0)
	{
	    theList.setSelectedIndex(0);
	}
    }

    @Override
    public void setEnabled(boolean value)
    {
	theList.setEnabled(value);
    }
    
    public boolean isEnabled()
    {
	return theList.isEnabled();
    }
    
    public void setVisible(boolean value)
    {
	theList.setVisible(value);
    }
    
}
